<?php
/* 
 * Copyright (c) 2018-2021   All rights reserved.
 * 
 * 创建时间：2021-05-17 11:01
 *
 * 项目：upload  -  $  - FormsTable.php
 *
 * 作者：liwei 
 */

namespace modules\levfm\table;

use Lev;
use lev\base\Modelv;
use lev\base\SubModelv;
use modules\levfm\helpers\levfmSetHelper;
use modules\levfm\table\form\Form_levfm_forms\Form_levfm_forms;
use modules\levfm\widgets\inputs\inputsFormWidget;

!defined('INLEV') && exit('Access Denied LEV');


class LevfmForms extends SubModelv
{

    public static $tableName = '{{%levfm_forms}}';

    /**
     * 库中建表名称统一格式
     * @param $id
     * @return string
     */
    public static function formTablename($id) {
        return 'levfm_form_'.$id;
    }

    /**
     * 表中字段名称统一格式
     * @param $id
     * @return string
     */
    public static function tableFieldname($id) {
        return '_'.$id;
    }

    public static function listShowType() {
        return [
            '分页显示',
        ];
    }

    public static function srhkeyType() {
        return [
            0 => '自定义且不能少于4个字符',
            1 => '使用创建时日期(年月日Ymd)',
        ];
    }

    public static function formTmp() {
        return [
            '默认模板',
            '投票模板（仅限select下拉性字段）'
        ];
    }

    /**
     * 表单所有权限字段
     * @return mixed
     */
    public static function getAccessInputs() {
        return Form_levfm_forms::formAccessInputs();
    }

    /**
     * 投票表单
     * @param $settings
     * @return bool
     */
    public static function isVoteForm($settings) {
        return static::getSettingsFormTmp($settings) == '1';
    }

    /**
     * 统计近N天数据
     * @param $settings
     * @return array|mixed|null
     */
    public static function isTotalitemDays($settings) {
        return ($day = intval(Lev::getSettings($settings, '_totalitem_days'))) >0 ? $day : 0;
    }

    /**
     * 统计展示数据自增
     * @param $settings
     * @return array|mixed|null
     */
    public static function getTotalitemTempnum($settings) {
        return floatval(Lev::getSettings($settings, '_totalitem_tempnum'));
    }

    /**
     * 允许重复投票
     * @param $settings
     * @return array|mixed|null
     */
    public static function isTotalitemRepeat($settings) {
        return !Lev::getSettings($settings, '_totalitem_repeat');
    }

    /**
     * 表单模板
     * @param $settings
     * @return int
     */
    public static function getSettingsFormTmp($settings) {
        return intval(static::getSettingsFormLists($settings)['formTmp']);
    }

    /**
     * 表单分页大小
     * @param $settings
     * @return int
     */
    public static function getSettingsPageNum($settings) {
        return intval(static::getSettingsFormLists($settings)['pageNum']) ?: levfmSetHelper::listPageNum();
    }

    /**
     * 表单列表相关设置
     * @param $settings
     * @return mixed
     */
    public static function getSettingsFormLists($settings) {
        return Lev::getSettings(Lev::getSettings($settings, '_formLists'))[0];
    }

    /**
     * 搜索关键词 追字段值
     * @param $settings
     * @return array|mixed|null
     */
    public static function getSettingsListShowFields($settings) {
        return json_decode(Lev::getSettings($settings, '_listShowFields'), true);
    }

    /**
     * 更新时间分类搜索
     * @param $formInfo
     * @return array
     */
    public static function getDayTypeSearchs($formInfo)
    {
        $arr = [];

        if ($_srh_day = Lev::getSettings(Lev::getSettings($formInfo['settings'], '_srh_day'))) {
            $input = Form_levfm_forms::getInputInfo('settings___srh_day', 5);
            $item = ['id'=>'day', 'title'=>'日期'];
            foreach ($_srh_day as $v) {
                if (empty($v['status'])) {
                    $day = -floatval($v['day']);
                    $item['options'][$v['day']] = static::setDayTimeToName($v['name'], $day, inputsFormWidget::getSelectItem($input));
                }
            }
            !empty($item['options']) && $arr['day'] = $item;
        }
        if ($_srh_day = Lev::getSettings(Lev::getSettings($formInfo['settings'], '_srh_days'))) {
            $input = Form_levfm_forms::getInputInfo('settings___srh_days', 5);
            $item = ['id'=>'days', 'title'=>'截止日期'];
            foreach ($_srh_day as $v) {
                if (empty($v['status'])) {
                    $day = -floatval($v['days']);
                    $pre = '后';
                    $item['options'][$v['days']] = static::setDayTimeToName($v['name'], $day, inputsFormWidget::getSelectItem($input), $pre);
                }
            }
            !empty($item['options']) && $arr['days'] = $item;
        }

        return $arr;
    }
    public static function setDayTime($day) {
        return Lev::getDayStartTime() + 24 * 3600 * $day;
    }
    public static function setDayTimeToName($name, $day, $options = [], $pre = '') {
        return date(Lev::arrv($name, $options, $name.$pre), static::setDayTime($day));
    }
    public static function setname() {
        return [
            'Ymd号' => '年月日(Ymd号)',
            'Y-m-d' => '年-月-日(Y-m-d)',
            'md号' => '月日(md号)',
            'm月d号' => '月日(m月d号)',
            'd号' => '日(d号)',
        ];
    }


    /**
     * 分类搜索字段转ID
     * @param $settings
     * @param $inputs
     * @return mixed
     */
    public static function getSettingsTypeSearchsToId($settings, $inputs) {
        $arr = static::getSettingsTypeSearchs($settings);
        if (!empty($arr)) {
            foreach ($arr as $k => $v) {
                $arr[$k] = $inputs[is_numeric($v) ? static::tableFieldname($v) : $v];
            }
        }
        return $arr;
    }

    /**
     * 分类搜索字段
     * @param $settings
     * @return mixed
     */
    public static function getSettingsTypeSearchs($settings) {
        return json_decode(Lev::getSettings($settings, '_typeSearchs'), true);
    }

    /**
     * 统计字段
     * @param $settings
     * @return array|mixed|null
     */
    public static function getSettingsTotalitem($settings) {
        return json_decode(Lev::getSettings($settings, '_totalitem'), true);
    }

    /**
     * 字段分类
     * @param $settings
     * @return array|mixed|null
     */
    public static function getSettingsClassify($settings) {
        return Lev::getSettings(Lev::getSettings($settings, '_fieldType'));
    }

    /**
     * 提取字段自定义分类
     * @param $formid
     * @param null $fields
     * @return array
     */
    public static function getMyselfClassify($formid, $fields = null) {
        $arr = [];

        $fields === null &&
        $fields = LevfmFields::findAllField('classify,id', ['formid'=>$formid]);
        if ($fields) {
            foreach ($fields as $v) {
                $arr[$v['classify']] = $v['classify'];
            }
        }

        return $arr;
    }

    /**
     * 更新表单数据总量
     * @param $formInfo
     * @param int $num
     */
    public static function updateTotalnum($formInfo, $num = 1)
    {
        is_numeric($formInfo) && $formInfo = static::findOne(['id'=>$formInfo]);
        if ($num === null) {
            $totalnum = LevfmDatas::total(['formid'=>$formInfo['id']]);
        }else {
            $totalnum = $formInfo['totalnum']+$num;
        }
        static::update(['totalnum'=>$totalnum], ['id'=>$formInfo['id']]);
    }

}